
<template>
  <!-- 新增部门的弹层 -->
  <el-dialog
    :title="form.id ? '编辑部门' : '新增部门'"
    :visible="showdialog"
    @close="closePop"
  >
    <el-form ref="addForm" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="form.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="form.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="form.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in managers"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" size="small" @click="submitForm">
        确定
      </el-button>
      <el-button size="small" @click="$emit('update:showdialog', false)">
        取消
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'

export default {
  props: {
    showdialog: {
      type: Boolean,
      default: false
    },
    currentpart: {
      type: Object,
      default: () => ({})
    },
    depts: {
      type: Array,
      default: () => ({})
    }

  },
  // 需要传入一个props变量来控制 显示或者隐藏
  data () {
    const valadateCode = (rules, value, callback) => {
      let flag
      if (this.form.id) {
        flag = this.depts.some(item => item.code === value && value !== this.currentpart.code)
      } else {
        flag = this.depts.some(item => item.code === value)
      }
      if (flag) {
        callback(new Error('重复的code'))
      }
      callback()
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: valadateCode, trigger: ['blur', 'change'] }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      },
      managers: []
    }
  },
  created () {
    this.getEmployeesData()
  },
  methods: {
    async getDetail (id) {
      try {
        const res = await getDepartDetail(id)
        this.form = res
      } catch (error) {
        console.log(error)
      }
    },
    async getEmployeesData () {
      const res = await getEmployeeSimple()
      this.managers = res
    },
    async submitForm () {
      try {
        await this.$refs.addForm.validate()
        if (this.form.id) {
          await updateDepartments(this.form)
        } else {
          await addDepartments({ ...this.form, pid: this.currentpart.id || '' })
        }

        this.$emit('update:showdialog', false)
        this.$emit('update-depart')
        this.$message.success('操作成功')
      } catch (error) {
        console.log(error)
      }
    },
    closePop () {
      this.$emit('update:showdialog', false)
      this.$refs.addForm.resetFields()
      this.form = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
    }
  }
}
</script>
